import { _decorator, Button, Label, Node, Sprite } from 'cc';
import { UIComponent } from '../../../Framework/Scripts/Managers/UIComponent';
import { UIMgr, UIType } from '../../../Framework/Scripts/Managers/UIMgr';
import { AbNames, EventMsgKes } from '../Config/ResConfig';
import { CocosUtils } from '../../../Framework/Scripts/Utils/CocosUtils';
import { AudioCfg, AudioMgr } from '../../../Framework/Scripts/Managers/AudioMgr';
import { NewPlayer_Ctr } from './NewPlayer_Ctr';
import { GameHYInvite_Ctr } from './GameHY/GameHYInvite_Ctr';
import { WECHAT } from 'cc/env';
import { wx } from '../../../Framework/Scripts/Platform/PlatformWX';
import { GameHallRes, UpdateUserInfoNotice } from '../../AssetPackage/Props/Hall/Hall';
import { GetUserRewardRes } from '../../AssetPackage/Props/Hall/GetUserReward';
import { FriendJoinNotify } from '../../AssetPackage/Props/Game/DT_PK';
import { GameData } from '../Data/GameData';
import { IUserInfo } from '../Config/CommonProto';
import { IHallRes } from './GamHall/GameHallProto';
const { ccclass, property } = _decorator;

@ccclass('GameHall_Ctr')
export class GameHall_Ctr extends UIComponent {
    spHead: Sprite;
    labName: Label;
    labScore: Label;
    bg: Node;

    protected onLoad(): void {
        this.InitUI();
    }

    start() {
        this.InitData();
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();
        this.setBgAni();

        this.bg = this.ViewNode('bg');

        this.spHead = this.ViewComponent('head/headbg/spHead',Sprite);
        this.labName = this.ViewComponent('labName',Label);
        this.labScore = this.ViewComponent('labScore',Label);

        this.AddButtonListener('head',this,this.onHeadClick);

        this.AddButtonListener('scoreBg/btnAdd',this,this.onBtnHD2Click);
        this.AddButtonListener('btnSet',this,this.onBtnSetClick);

        this.AddButtonListener('TopBtns/btn1',this,this.onBtnHD1Click);
        this.AddButtonListener('TopBtns/btn2',this,this.onBtnHD2Click);
        this.AddButtonListener('TopBtns/btn3',this,this.onBtnHD3Click);
        this.AddButtonListener('TopBtns/btn4',this,this.onBtnHD4Click);
        this.AddButtonListener('TopBtns/btn5',this,this.onBtnHD5Click);

        this.AddButtonListener('bgDown/btnStart',this,this.onBtnStartClick);
        this.AddButtonListener('bgDown/btnPK',this,this.onBtnPKClick);
        this.AddButtonListener('bgDown/btnCJ',this,this.onBtnCJClick);
    }

    private InitData(){
        //微信，拿取微信的分享数据，url 里面带的参数
        if(WECHAT){
            wx.onShow((res) => {
                console.log('onShow:', res);
                const query = res.query;
                // 在这里处理分享链接中的参数
            });
    
            const launchOptions = wx.getLaunchOptionsSync();
            console.log('getLaunchOptionsSync:', launchOptions);
            const query = launchOptions.query;
            // 在这里处理分享链接中的参数

            console.log('=======在这里处理分享链接中的参数=======',query);
        }

        //播放音乐
        let isOpen = CocosUtils.localStorageGet(AudioCfg.sound_musicKey);
        if(isOpen == 1 || isOpen == undefined || isOpen == null){
            AudioMgr.inst.play(AbNames.Common,'Sounds/bgm');
            CocosUtils.localStorageSet(AudioCfg.sound_musicKey,1);
            CocosUtils.localStorageSet(AudioCfg.sound_effectKey,1);
        }

        this.setUserInfo(GameData.inst.userInfo);

        //请求大厅数据
        globalThis.gameMgr._GameHallMsg.GameHallReq();

        // this.onNetConnect();
    }

    private registerEvent(){
        //  globalThis.gameMgr._eventTarget.on(WSConnectedEvent,this.onNetConnect,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGameHallRes,this.onGameHallRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgGetUserRewardRes,this.onGetUserRewardRes,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgUpdateUserInfoNotice,this.onUpdateUserInfoBroadcast,this);
        globalThis.gameMgr._eventTarget.on(EventMsgKes.msgFriendJoinNotify,this.onFriendJoinNotify,this);
    }

    private unRegisterEvent(){
        //  globalThis.gameMgr._eventTarget.off(WSConnectedEvent,this.onNetConnect,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGameHallRes,this.onGameHallRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgGetUserRewardRes,this.onGetUserRewardRes,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgUpdateUserInfoNotice,this.onUpdateUserInfoBroadcast,this);
        globalThis.gameMgr._eventTarget.off(EventMsgKes.msgFriendJoinNotify,this.onFriendJoinNotify,this);
    }

    private setUserInfo(user: IUserInfo){;
        if(this.hasProInObj(user,'avator')){
            // user.avator = 'https://weparty-1252406287.cos.ap-chongqing.myqclo…atch/1/91bce913c64c425c891ab7be1fd30f97head10.jpg';
            this.LoadRemoteImage(user.avator,this.spHead);
        }
        if(this.hasProInObj(user,'name')){
            this.labName.string = user.name;
        }

        let num = 0;
        if(this.hasProInObj(user,'ballNum')){
            num = user.ballNum
        }
        this.labScore.string = num.toString();
    }

    //点击头像
    private onHeadClick(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameUserInfo',UIType.Full);
    }

    //点击设置
    private onBtnSetClick(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameSet',UIType.UnFull);
    }

    //点击排行
    private onBtnHD1Click(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameRank/GameRank',UIType.Full);
    }

    //点击福利活动
    private onBtnHD2Click(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/HD/GameHD2',UIType.Full);
    }

    //点击登录奖励
    private onBtnHD3Click(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/HD/GameHD3',UIType.Full);
    }

    //点击惊喜宝箱
    private onBtnHD4Click(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/HD/GameHD4',UIType.UnFull);
    }

    //点击幸运转盘
    private onBtnHD5Click(btn: Button){
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/HD/GameHD5',UIType.Full);
    }

    //点击分类主题挑战赛
    private onBtnStartClick(btn: Button) {
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameStart1/GameStart1',UIType.Full);
    }

    //点击好友PK
    private onBtnPKClick(btn: Button) {
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameHY/GameHY',UIType.Full);
    }

    // 点击每日挑战
    private onBtnCJClick(btn: Button) {
        UIMgr.inst.ShowUIView(AbNames.GameHall, 'Prefabs/GameMRTZ/GameMRTZ',UIType.Full);
    }

    private onNetConnect(){
        console.log('=======链接大厅服务成功=========');
        //请求大厅数据
        globalThis.gameMgr._GameHallMsg.GameHallReq();
    }

    //请求大厅返回
    private onGameHallRes(data: IHallRes) {
        console.log('========onGameHallRes==========',data);
        if(data){
            if(this.hasProInObj(data,'isNewPlayer')){
                let isNew = data.isNewPlayer;
                if(isNew == 1){ //1 是新用户 2 不是
                    //请求奖励的数据
                    globalThis.gameMgr._GameHallMsg.GetUserRewardReq();
                }
            }

            let user = data.userInfo;
            if(user){
                this.setUserInfo(user);
            }
        }
    }

    //获取用户奖励的数据成功
    private onGetUserRewardRes(data: GetUserRewardRes){
        if(this.hasProInObj(data,'rewards')){
            let com = UIMgr.inst.ShowUIView(AbNames.GameHall,'Prefabs/NewPlayer',2) as NewPlayer_Ctr;
            com.setData(data.rewards);
        }
    }

    //用户数据发生变化广播
    private onUpdateUserInfoBroadcast(data:any){
        console.log('==============onUpdateUserInfoBroadcast=========',data);
        let user = data.userInfo;
        if(user){
            this.setUserInfo(user);
        }
    }

    //有用户加入的通知
    private onFriendJoinNotify(data: FriendJoinNotify){
        console.log('==============onFriendJoinNotify=========',data);
        let ts_node = UIMgr.inst.ShowUIView(AbNames.GameHall,'Prefabs/GameHY/GameHYInvite',0) as GameHYInvite_Ctr ;
        ts_node.setData(data);
    }   
}